import { Component, OnInit, ChangeDetectorRef, ViewChild, Inject, forwardRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { ThreeAdvertisingGridComponent } from './three-advertising-grid/three-advertising-grid.component';
import { ThreeAdvertisingAddComponent } from './three-advertising-add/three-advertising-add.component';
import { masterService } from '../../../../lib/master.service';
import { saveVarService } from '../../../../lib/saveVar.service';
import { path } from '../../../path.config';
import { AppComponent } from '../../../app.component';

@Component({
    selector: 'app-three-advertising-management',
    templateUrl: './three-advertising-management.component.html',
    styleUrls: ['./three-advertising-management.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class ThreeAdvertisingManagementComponent implements OnInit {
    @ViewChild('threeAdGrid') private threeAdGrid: ThreeAdvertisingGridComponent;
    @ViewChild('threeAdAdd') private threeAdAdd: ThreeAdvertisingAddComponent;
    // api
    // 获取列表项api
    private getList = path.advertising.threeAdvertising.getList;
    // 位置类型下拉框
    private selType: any = path.advertising.threeAdvertising.getSettingType;


    // 搜索传参的对象
    public filter: any = {
        type: '',
        adName: '',
        pageSize: '10',
        pageIndex: '1'
    }

    public options: Array<any> = [];

    constructor(private _masterService: masterService,
        private _saveVarService: saveVarService,
        private cdRef: ChangeDetectorRef,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    public ngOnInit(): void {
        // 获取位置类型选项值
        this._masterService
            .getData(this.selType)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.options = res.data.items;
                    this.threeAdAdd.options = res.data.items;
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    // 打开新建模态框
    public openAddModal() {
        this.threeAdAdd.newThreeAd.show();
    }


    // 过滤grid数据
    selectListItem() {
        this.threeAdGrid.onChangeTable(this.filter, '1');
    }

    threeAdAddFunc() {
        this.filter.type = '';
        this.filter.adName = '';
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.threeAdGrid.onChangeTable();
                } else if (res.statusCode == 401) {
                    this.app.logOff();
                } else {
                    this.app.toastError('错误信息: ' + res.errorMessage);
                }
            })
    }
}
